package com.int_tech.user.servise;

import com.int_tech.user.dao.ClientDao;
import com.int_tech.user.dao.UserDao;
import com.int_tech.user.entity.Client;
import com.int_tech.user.entity.User;
import com.int_tech.utils.CarEnum;
import com.int_tech.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;


/**
 * Created by Administrator on 2017/9/21.
 */
@Service
public class UserService{

    @Autowired
    private UserDao userDao;

    //分页显示条数
    @Value("${size}")
    private int size;

    @Autowired
    private ClientDao clientDao;

    /**
    * TODO 添加用户信息
    * @param user 用户
    * @return Result
    * @author dmy
    */
    public Result saveUser(User user){
        try {
                //根据用户名和密码验证用户是否存在
        		if (userDao.findUserByNameAndPasswordAndIsDel(user.getName(),user.getPassword(), CarEnum.ON.getCode())!=null){
                    return Result.build(500,"用户已存在");
                } else if(user.getType()==2){
        		    //如果用户身份为普通用户，关联客户id不能为空
                    return Result.build(500,"用户关联客户id不能为空");
                } else {
        		    user.setCreateTime(new Date());
        		    user.setUpdateTime(new Date());
        		    userDao.save(user);
        		    return Result.ok();
                }
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
        	return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据用户名和密码验证用户是否存在
    * @param name,password 用户名 密码
    * @return Result
    * @author dmy
    */
    public Result findUserByNameAndPassword(String name,String password){
        try {
            User user = userDao.findUserByNameAndPasswordAndIsDel(name, password,CarEnum.ON.getCode());
            if (user!=null){
                return Result.build(201,"用户已存在");
            }else {
                return Result.ok();
            }
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据用户名和user类型验证用户是否存在
    * @param name,type 用户名，用户类型
    * @return Result
    * @author dmy
    */
    public Result findUserByNameAndType(String name,int type){
        try {
            User user = userDao.findUserByNameAndTypeAndIsDel(name, type,CarEnum.ON.getCode());
            if (user!=null){
                return Result.build(201,"用户已存在");
            }else {
                return Result.ok();
            }
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据用户id删除(逻辑删除）
    * @param id 用户id
    * @return Result
    * @author dmy
    */
    @Transactional
    public Result deleteUserById(Integer id){
        try {
            User user = userDao.findOne(id);
            user.setIsDel(CarEnum.OFF.getCode());
            userDao.save(user);
            return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    public Result updateUser(User user){
        try {
                user.setUpdateTime(new Date());
        		userDao.save(user);
        		return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据用户id的查询用户信息
    * @param id 用户id
    * @return Result(User)
    * @author dmy
    */
    public Result findUserByid(Integer id){
        try {
            return Result.ok(userDao.findOne(id));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 查询所有用户
    * @return Result(List<User>)
    * @author dmy
    */
    public Result findAllUser(){
        try {
             return Result.ok(userDao.findUserByIsDel(CarEnum.ON.getCode()));
        	} catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return Result.build(500, "异常");
        }
    }

    /**
    * TODO 分页查询所有用户
    * @param currentPage 当前页
    * @return Result(List<User>)
    * @author dmy
    */
    public Result fingByPage(int currentPage){
        try {
            PageRequest pageRequest = new PageRequest(currentPage-1,size);
            return Result.ok(userDao.findUserByIsDel(pageRequest, CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据id集合查询用户信息
    * @param Ids
    * @return Result(List<User>)
    * @author dmy
    */
    public Result findByIdIn(List<Integer>Ids){
        try {
           return Result.ok(userDao.findByIdInAndIsDel(Ids,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
        	return Result.build(500,"异常");
        }

    }

    public List<User> findByClientId(String id) {
        return userDao.findByClientId(id);
    }

    public User findByNameAndLegalPerson(String name, String legalPerson) {
        return userDao.findByNameAndLegalPerson(name,legalPerson);
    }

    public void save(User user) {
        userDao.save(user);
    }

    public List<User> findByClientIdAndType(String clientId, int i) {
        return userDao.findByClientIdAndType(clientId,i);
    }

    public Page<Client> findByClientId(int page,int size){
        PageRequest pageRequest = new PageRequest(page - 1, size);
        return clientDao.findByIsDel(pageRequest,CarEnum.ON.getCode());
    }

    public Page<User> findByClientId(String clientId,int page,int size){
        PageRequest pageRequest = new PageRequest(page - 1, size);
        return userDao.findByClientIdAndIsDel(pageRequest,clientId,CarEnum.ON.getCode());
    }

    public Page<User> findUser(int page,int size){
        PageRequest pageRequest = new PageRequest(page - 1, size);
        return userDao.findByIsDel(pageRequest,CarEnum.ON.getCode());
    }
}
